package common;

import java.io.IOException;

import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import pojo.FastDFSFile;

public class FileManager implements FileManagerConfig {

	/**   
	 * @Fields serialVersionUID : TODO
	 */   
	private static final long serialVersionUID = 1L;
	
    private static TrackerClient trackerClient;
    private static TrackerServer trackerServer;
    private static StorageServer storageServer;
    private static StorageClient1 storageClient;
    
	   public FileManager(String conf) throws Exception {
		      ClientGlobal.init(conf);
		      trackerClient = new TrackerClient();
		      trackerServer = trackerClient.getConnection();
		      storageServer = null;
		      storageClient = new StorageClient1(trackerServer, storageServer);
		   }
	   /**
	    * 
	    * @Title: upload   
	    * @Description: 文件上传
	    * @param: @param file
	    * @param: @param valuePairs
	    * @param: @return      
	    * @return: String      
	    * @throws
	    */
	   public String upload(FastDFSFile file,NameValuePair[] valuePairs) {
	        String[] uploadResults = null;
	        try {
	            uploadResults = storageClient.upload_file(file.getContent(),file.getExt(), valuePairs);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        String groupName = uploadResults[0];
	        String remoteFileName = uploadResults[1];

	        String fileAbsolutePath = PROTOCOL
	                + TRACKER_NGNIX_ADDR+":"
	                + TRACKER_NGNIX_PORT 
	                + SEPARATOR + groupName
	                + SEPARATOR + remoteFileName;
	        return fileAbsolutePath;
	    }	   
	   
	   /**
	    * 
	    * @param String groupName
	    * @param String  remoteFileName
	    * @return returned value comment here
	    * @author jiao
	    */
	   public static ResponseEntity<byte[]> download(String groupName,String remoteFileName,String specFileName) {
	       byte[] content = null;
	       HttpHeaders headers = new HttpHeaders();
	       try {
	           content = storageClient.download_file(groupName, remoteFileName);
	           headers.setContentDispositionFormData("attachment",  new String(specFileName.getBytes("UTF-8"),"iso-8859-1"));
	           headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
	       } catch (Exception e) {
	           // TODO Auto-generated catch block
	           e.printStackTrace();
	       }
	       return new ResponseEntity<byte[]>(content, headers, HttpStatus.CREATED);
	   }
	   
	   /**
	    * 
	    * @Title: delete_file   
	    * @Description: 根据真实文件路径删除文件，删除成功返回
	    * @param: @param storagePath
	    * @param: @return      
	    * @return: Integer      
	    * @throws
	    */
	   public Integer delete_file(String storagePath){  
	        int result=-1;
	        String realPath=storagePath.substring(28, storagePath.length());
	        System.out.println(realPath);
	        try {  
	            result = storageClient.delete_file1(realPath);  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        } catch (MyException e) {  
	            e.printStackTrace();  
	        }  
	        return  result;  
	    }  
}
